package model;

import java.sql.*;
import java.util.ArrayList;

import myutil.Connect;

import vo.LoginInfoVo;
import vo.loggerVo;

/**
 * @author Sony
 * 
 */

public class LoginMo {
	ArrayList<LoginInfoVo> loginInfo = null;

	String image_path;
	public ArrayList<LoginInfoVo> authenticate(String username,
			String password, String site) throws Exception {
		// String site = null;
		Connect c = new Connect();
		c.site = site;
		String user_type = "";
		String searchLoginDetailsQuery;
		searchLoginDetailsQuery = "select * from login_info where login_id='"
				+ username + "' and password='" + password + "'";
//System.out.println(searchLoginDetailsQuery);
		ResultSet rs = c.getResult(searchLoginDetailsQuery);
		if (rs.next()) {
			loginInfo = new ArrayList<LoginInfoVo>();
			rs.previous();
		}

		while (rs.next()) {
			LoginInfoVo logininfo = new LoginInfoVo();
			logininfo.setLogin_id(rs.getString("login_id"));
			logininfo.setUser_type(rs.getString("user_type"));
			user_type=rs.getString("user_type");
			
			String query;
			if (user_type.equals("student")) {
				query="select student_id, student_name from student_details where email_id='"+username+"'";
				//System.out.println(query);
				ResultSet rs2 = c.getResult(query);
				while(rs2.next()){
				logininfo.setUser_id(rs2.getInt("student_id"));
				logininfo.setUser_name(rs2.getString(2));
				}
				
			}
			else if(user_type.equals("professor"))
			{
				query="select staff_id, staff_name from college_staff where email_id='"+username+"'";
				ResultSet rs2 = c.getResult(query);
				while(rs2.next()){
				logininfo.setUser_id(rs2.getInt("staff_id"));
				logininfo.setUser_name(rs2.getString(2));
				}
			}
			else if(user_type.equals("admin"))
			{
				logininfo.setUser_id(0);
				logininfo.setUser_name("IIIT Bangalore");
			}
			loginInfo.add(logininfo);
			System.out.println("added the login info");
		
		}

		

		return loginInfo;

	}
	
	public ArrayList<LoginInfoVo> showImage(String username, String site){
		ArrayList<LoginInfoVo> imagelist=new ArrayList<LoginInfoVo>();
		Connect c = new Connect();
		c.site = site;
		String query;
		query="select * from profile_picture where user_id='"+username+"'";
		System.out.println(query);
		try {
			ResultSet rs=c.getResult(query);
			while(rs.next())
			{
				LoginInfoVo vo=new LoginInfoVo();
				String image_name=rs.getString("picture_name");
				
				image_path="./attachments/"+username+"/"+image_name;
				vo.setImage(image_path);
				System.out.println(image_path);
				imagelist.add(vo);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return imagelist;
	}
	
	public ArrayList<loggerVo> getloggers(){
		ArrayList<loggerVo> loggerlist= new ArrayList<loggerVo>();
		String query="";
		Connect c= new Connect();
		query="select section,operation,user,execute_time from logger order by execute_time desc";
		System.out.println(query);
		try {
			ResultSet rs=c.getResult(query);
			while(rs.next()){
				loggerVo vo = new loggerVo();
				String section=rs.getString("section");
				String operation=rs.getString("operation");
				String user=rs.getString("user");
				String execute_time=rs.getString("execute_time");
				String log="-- "+operation+" has been performed on "+section+" by "+ user +" at  "+execute_time;
				vo.setLog(log);
				loggerlist.add(vo);
				System.out.println(log);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return loggerlist;
	}
	
	public ArrayList<LoginInfoVo> getLoginInfo() {
		return loginInfo;
	}
	public void setLoginInfo(ArrayList<LoginInfoVo> loginInfo) {
		this.loginInfo = loginInfo;
	}
	public String getImage_path() {
		return image_path;
	}
	public void setImage_path(String image_path) {
		this.image_path = image_path;
	}
}
